package net.steam.easyjoboa.biz.impl;

import java.util.ArrayList;
import java.util.List;

import net.steam.easyjoboa.biz.MessageBiz;
import net.steam.easyjoboa.dao.MessageDao;
import net.steam.easyjoboa.entity.Message;
import net.steam.easyjoboa.entity.PageBeans;

public class MessageBizImpl implements MessageBiz {

	private MessageDao messageDao;	
	
	public void setMessageDao(MessageDao messageDao) {
		this.messageDao = messageDao;
	}


	/**
	 * 发送消息
	 */
	public int addMessage(Message entity) {
		int i = 0;
		if (entity.getMesTitle().length()>0 && entity.getContent().length()>0) {			
			try {			    
			  i = (Integer) messageDao.addEntity(entity);
			} catch (Exception e) {
				e.printStackTrace();
			}			
		}
		return i;
	}

	
	/**
	 * 鏍规嵁鐧婚檰鑰匢D
	 * 鍒嗛〉鏌ヨ鑷繁鎵�彂淇′欢
	 */
	public List<Message> findOutBox(PageBeans pageBean, Message message,int id) {
		List<Object> params = new ArrayList<Object>();
		String hql = "from Message m where m.sendStatus = '0' and m.employeeBySendId.userId="+id;	
		if (message!=null) {
			hql += " and m.employeeByGetId.userName =?";
			params.add(message.getEmployeeByGetId().getUserName());
		}
		hql +=" order by m.sendTime desc";
		return messageDao.findByPage(pageBean, hql, params.toArray());		
		 
	}

	/**
	 * 鏍规嵁鐧婚檰鑰匢D
	 * 鍒嗛〉鏌ヨ鑷繁鎵�敹鐨勬湭璇讳俊浠�
	 */
	public List<Message> findInBoxNoRead(PageBeans pageBean, Message message,int id) {
		List<Object> params = new ArrayList<Object>();
		String hql = "from Message m where m.mesState='1' and getStatus = '0' and m.employeeByGetId.userId="+id;	
		if (message!=null) {
			if (message.getEmployeeBySendId().getUserName().trim().length()>0 && message.getEmployeeBySendId().getUserName().trim()!=null) {
				hql += " and m.employeeBySendId.userName =?";
				params.add(message.getEmployeeBySendId().getUserName());
			}
		}
		hql +=" order by m.sendTime desc";
		return messageDao.findByPage(pageBean, hql, params.toArray());
	}
	
	/**
	 * 鏍规嵁鐧婚檰鑰匢D
	 * 鍒嗛〉鏌ヨ鑷繁鎵�敹鐨勫凡璇讳俊浠�
	 */
	public List<Message> findInBoxRead(PageBeans pageBean, Message message,
			int id) {
		List<Object> params = new ArrayList<Object>();
		String hql = "from Message m where m.mesState='0' and getStatus = '0' and m.employeeByGetId.userId="+id;	
		if (message!=null) {
			if (message.getEmployeeBySendId().getUserName().trim().length()>0 && message.getEmployeeBySendId().getUserName().trim()!=null) {
				hql += " and m.employeeBySendId.userName =?";
				params.add(message.getEmployeeBySendId().getUserName());
			}			
		}
		hql +=" order by m.sendTime desc";
		return messageDao.findByPage(pageBean, hql, params.toArray());		
		
	}

	/**
	 * 鎶婃湭璇讳俊浠舵敼涓哄凡璇�
	 */
	public void updateMessage(Message message) {
		message.setMesState("0");
		messageDao.updateEntity(message);
		
	}
	
	/**
	 * 鏍规嵁淇′欢ID杩涜闃呰
	 */
	public Message getMessage(int id) {		
		return messageDao.findById(id);
	}

	/**
	 * 鏍规嵁閫変腑鐨勪俊浠禝D杩涜閫昏緫鍒犻櫎
	 * @throws Exception 
	 */	
	public void delMessageGet(int[] id){
		try {
			for (int i = 0; i < id.length; i++) {
				int j = id[i];
				Message message = messageDao.findById(j);
				message.setGetStatus("1");
				messageDao.updateEntity(message);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}		
	}

	/**
	 * 鍒犻櫎鍙戜俊绠变腑鎵��鎷╃殑淇′欢
	 * @param id
	 */
	public void delMessageSend(int[] id){
		try {
			for (int i = 0; i < id.length; i++) {
				int j = id[i];
				Message message = messageDao.findById(j);
				message.setSendStatus("1");
				messageDao.updateEntity(message);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}		
	}

	
}
